﻿#include "GuideBubble.h"
#include "ui_GuideBubble.h"

#include <QGraphicsDropShadowEffect>
#include <QDebug>

#define COLOR_SHADOW QColor(0x77, 0xCB, 0xFF, 0xDD)


GuideBubble::GuideBubble(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::GuideBubble)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_TranslucentBackground);
    setWindowFlags(Qt::FramelessWindowHint | Qt::Tool);

    connect(ui->btnPrev, &QToolButton::clicked, this, &GuideBubble::prevRequested);
    connect(ui->btnNext, &QToolButton::clicked, this, &GuideBubble::nextRequested);
    connect(ui->btnDone, &QToolButton::clicked, this, &GuideBubble::doneRequested);

    // 设置边框阴影
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    shadow->setBlurRadius(20);       // 模糊半径，值越大越柔和
    shadow->setColor(COLOR_SHADOW);  // 阴影颜色（含透明度）
    shadow->setOffset(0, 0);         // 阴影偏移（x, y）
    ui->container->setGraphicsEffect(shadow);
}

GuideBubble::~GuideBubble()
{
    delete ui;
}

void GuideBubble::setText(const QString &text)
{
    ui->labelTip->setText(text);
}

void GuideBubble::setStepCount(int count)
{
    m_stepCnt = count;
    ui->labelStepCnt->setNum(count);
}

void GuideBubble::setCurrentStep(int step)
{
    ui->labelCurrentStep->setNum(step);
    if (step <= 1) {
        ui->btnPrev->setEnabled(false);
    } else if (step >= m_stepCnt) {
        ui->btnNext->setEnabled(false);
    } else {
        ui->btnNext->setEnabled(true);
        ui->btnPrev->setEnabled(true);
    }
}

void GuideBubble::setAnimationEnabled(bool enabled)
{
    m_animationEnabled = enabled;
}

void GuideBubble::setAnimationDuration(int ms)
{
    m_duration = ms;
}

void GuideBubble::move(int x, int y)
{
    move(QPoint(x, y));
}

void GuideBubble::move(const QPoint &p)
{
    QRect target = geometry();
    target.moveTopLeft(p);
    setGeometry(target); // 统一逻辑入口
}

void GuideBubble::resize(int w, int h)
{
    resize(QSize(w, h));
}

void GuideBubble::resize(const QSize &s)
{
    QRect target = geometry();
    target.setSize(s);
    setGeometry(target); // 统一逻辑入口
}

void GuideBubble::setGeometry(int x, int y, int w, int h)
{
    setGeometry(QRect(x, y, w, h));
}

void GuideBubble::setGeometry(const QRect &rect)
{
    if (!m_animationEnabled) {
        QWidget::setGeometry(rect);
        return;
    }

    QRect current = geometry();
    if (current == rect) {
        QWidget::setGeometry(rect);
        return;
    }

    animateTo(rect);
}

void GuideBubble::animateTo(const QRect &target)
{
    if (!m_anim)
        m_anim = new QPropertyAnimation(this, "geometry", this);

    m_anim->stop();
    m_anim->setDuration(m_duration);
    m_anim->setStartValue(geometry());
    m_anim->setEndValue(target);
    m_anim->setEasingCurve(QEasingCurve::OutCubic);
    m_anim->start();
}

void GuideBubble::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_Right:
    case Qt::Key_Down:
    case Qt::Key_Space:
        emit nextRequested();
        break;
    case Qt::Key_Left:
    case Qt::Key_Up:
        emit prevRequested();
        break;
    case Qt::Key_Escape:
        hide();
        emit doneRequested();
        return;
    default:
        QWidget::keyPressEvent(event);
        return;
    }
}
